package com.zx.flow.db.typehandler;

import com.alibaba.fastjson.JSONArray;
import com.zx.flow.api.execute.model.FlowOperator;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

/**
 *
 */
public class OperatorsTypeHandler extends BaseTypeHandler<List<FlowOperator>> {

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, List<FlowOperator> e, JdbcType jdbcType)
            throws SQLException {
        preparedStatement.setString(i, e == null ? "[]" : JSONArray.toJSONString(e));
    }

    @Override
    public List<FlowOperator> getNullableResult(ResultSet resultSet, String s) throws SQLException {
        String data = resultSet.getString(s);
        return resultSet.wasNull() ? null : JSONArray.parseArray(data, FlowOperator.class);
    }

    @Override
    public List<FlowOperator> getNullableResult(ResultSet resultSet, int i) throws SQLException {
        String data = resultSet.getString(i);
        return resultSet.wasNull() ? null : JSONArray.parseArray(data, FlowOperator.class);
    }

    @Override
    public List<FlowOperator> getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        String data = callableStatement.getString(i);
        return callableStatement.wasNull() ? null : JSONArray.parseArray(data, FlowOperator.class);
    }
}